package com.mi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mi.entity.MyPages;
import com.mi.entity.ProductCategoryEntity;
import com.mi.entity.ProductEntity;
import com.mi.service.ProductCategoryService;
import com.mi.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ProductController {

    @Reference(check = false)
    ProductCategoryService productCategoryService;
    
    @Reference(check = false)
    ProductService productService;

    //产品中心
    @RequestMapping("product")
    public String toProductPage(ProductCategoryEntity pce ,ProductEntity pe,HttpServletRequest request){

        //调用service查询到所有类别的产品
        List<ProductCategoryEntity> ranks = productCategoryService.queryProductsRank();

        request.getSession().setAttribute("ranks",ranks);

        if(null != pce && pce.getPc_id() > 0) {     //pce不为空就根据pce带来的条件查询

            //根据条件查询不同类别的产品小类别
            List<ProductCategoryEntity> products = productCategoryService.queryProductsInfo(pce);

            if(pce.getPpc_id() > 0){
                pe.setP_name("notEmpty");
                pe.setPc_id(pce.getPpc_id());

                MyPages<ProductEntity> allProducts = productService.queryProducts(pe);

                request.setAttribute("ppc_id",pce.getPpc_id());
                request.setAttribute("allProducts",allProducts);
            }else {
                pe.setP_name("empty");
                MyPages<ProductEntity> allProducts = productService.queryProducts(pe);

                request.setAttribute("allProducts",allProducts);
            }


            request.setAttribute("pc_id",pce.getPc_id());

            request.setAttribute("products", products);
            request.setAttribute("pid", pce.getPc_id());

        } else {      //pce为空默认查询1号id展示

            pce.setPc_id(1);
            pe.setPc_id(1);
            pe.setP_name("empty");

            //根据条件查询不同类别的产品小类别
            List<ProductCategoryEntity> products = productCategoryService.queryProductsInfo(pce);

            request.setAttribute("products", products);
            request.setAttribute("pid", pce.getPc_id());

            //查询产品信息
            MyPages<ProductEntity> allProducts = productService.queryProducts(pe);

            request.setAttribute("allProducts",allProducts);

        }

        return "product";
    }

    //查询产品详情
    @RequestMapping("product_info")
    public String productInfo(int p_id,HttpServletRequest request){

        //根据产品id查询产品详情
        ProductEntity productEntity = productService.queryProduct(p_id);

        System.out.println(productEntity.getP_check());

        request.setAttribute("productEntity",productEntity);

        //调用方法将产品浏览量加1
        productService.updProductCheck(p_id);

        return "product_info";
    }


}
